<?php
/*
// Build a map structure for BCT
*/
class BCT
{
	// Store the current BCT
	var $bct_array;
	
	// Store the map BCT
	var $map;
	
	/*
	* function BCT define the current BCT depend on map BCT. If the map BCT isn't exist in session,
	*	it will get default map BCT
	*
	* @param string $id_bct indicates the index of the map BCT
	* @param string $id_parent indicates the parent index of the map BCT. 
	*	If the value is empty, class BCT will get parent default to build map
	*/

	function BCT($id_bct, $id_parent = '')
	{
		if (FRM_Session::exist('bct_map_serialize'))
		{
			$this->map = unserialize(FRM_Session::get('bct_map_serialize'));
		}
		else
		{
			if (FRM::load('package') == 'Admin')
			{
				$this->map = array(
					'home' => array(
						'title' => 'HOME',
						'id_parent_bct' => '',
						'url_default' => '/index/index/',
						'id_parent_ref_url' => '',
						'url' => '',
						'id' => '',
						'sql' => ''
					),
					'add_garage' => array(
							'title' => 'Add Garage',
							'id_parent_bct' => '',
							'url_default' => '/user/garage/',
							'id_parent_ref_url' => '',
							'url' => '',
							'id' => '',
							'sql' => ''
					),
					'garageholder' => array(
							'title' => 'Garageholder',
							'id_parent_bct' => '',
							'url_default' => '/admin/customer/',
							'id_parent_ref_url' => '',
							'url' => '',
							'id' => '',
							'sql' => ''
					),
					'view_garageholder' => array(
							'title' => 'View Garageholder',
							'id_parent_bct' => 'garageholder',
							'url_default' => '/admin/customer/',
							'id_parent_ref_url' => '',
							'url' => '',
							'id' => '',
							'sql' => ''
					),
					'garage' => array(
							'title' => 'Garage',
							'id_parent_bct' => 'admin',
							'url_default' => '/admin/garage/',
							'id_parent_ref_url' => '',
							'url' => '',
							'id' => '',
							'sql' => ''
					),
					'view_garage' => array(
							'title' => 'View Garage',
							'id_parent_bct' => 'garage',
							'url_default' => '/admin/garage/',
							'id_parent_ref_url' => '/admin/garage/',
							'url' => '',
							'id' => '',
							'sql' => ''
					),
				);
			}
		}
		
		$this->bct_array = '';
		$this->buildBCT($id_bct, $id_parent, true);
		$this->bct_array = array_reverse($this->bct_array);
		
		$this->setID();

		FRM_Session::set('bct_map_serialize', serialize($this->map));
		
	}
	
	/*
	* function buildBCT define the map BCT. If the map isn't exist in session,
	*	it will get default map
	*
	* @param string $id_bct indicates the index of the map BCT
	* @param string $id_parent indicates the parent index of the map BCT. 
	*		If the value is empty, class BCT will get parent default to build map
	* @param boolean $get_current_url indicates get current URL
	*	If true : get current URL for current id_bct
	*	If false : not get current URL for current id_bct
	*/
	private function buildBCT($id_bct, $id_parent = '', $get_current_url = false)
	{
		
		if ($get_current_url)
		{	
			$url = str_replace('qlbh/', '', $_SERVER['REQUEST_URI']);
			$url = str_replace('export/1', '', $url);
		
			$this->map[$id_bct]['url'] = APPLICATION_URL . $url;
			$this->map[$id_bct]['id_parent_ref_url'] = $id_parent;
		}
		$this->bct_array[] = $this->map[$id_bct];
		
		if ($this->map[$id_bct]['id_parent_ref_url'] != '')
			$this->buildBCT($this->map[$id_bct]['id_parent_ref_url']);
		else if (array_key_exists('id_parent_bct', $this->map[$id_bct]))
		{
			if (is_array($this->map[$id_bct]['id_parent_bct']))
				if ($id_parent != '')
					$this->buildBCT($id_parent);
				else
					$this->buildBCT($this->map[$id_bct]['id_parent_bct'][0]);
		}
		else
		{
			echo "BCT error: Key '$id_bct' is not existed in map BCT!";
		}
						
	}
	
	/*
	* When user go to page with direct URL. If URL views child page detail, 
	* 	BCT need to know the URL of parent page.
	* function setID gets id from database belong to sql in last element of $this->bct_array
	* 	and set this id to parent element in $this->bct_array. URL build from URL_default and id has just set
	*
	*/
	private function setID()
	{
	
		if (sizeof($this->bct_array) >= 2)
		{
			if ($this->bct_array[sizeof($this->bct_array)-2]['url'] == '' && $this->bct_array[sizeof($this->bct_array)-1]['sql'] != '')
			{
				$page = new Site();
				$page->query($this->bct_array[sizeof($this->bct_array)-1]['sql'] . FRM_Request::get('id'));
				$page->fetch();
				$dataArr = $page->toArray();
				for ($i = 0; $i < sizeof($this->bct_array) ; $i++)
				{
					if (array_key_exists('id_name_database',$this->bct_array[$i]))
						if (array_key_exists($this->bct_array[$i]['id_name_database'], $dataArr))
							$this->bct_array[$i]['url'] = $this->bct_array[$i]['url_default'] . "id/" . $dataArr[$this->bct_array[$i]['id_name_database']];
				}
			}
		}
		
		
	}
	
	/*
	* function getBackLink
	* Return the string URL for previous of current page
	*/
	public function getBackLink()
	{
		$url = ($this->bct_array[sizeof($this->bct_array)-2]['url'])? $this->bct_array[sizeof($this->bct_array)-2]['url'] : $this->bct_array[sizeof($this->bct_array)-2]['url_default'];
		
		return $url;
	}
	
	/*
	* function getBCT
	* Return the array of current BCT
	*/
	public function getBCT()
	{
		return $this->bct_array;
	}
	
	

}

?>
